package cz.proks.easyhelpdesk.gwt.restapi;

import java.util.Collection;

import org.jsonmaker.gwt.client.Jsonizer;
import org.spiffyui.client.rest.RESTCallback;
import org.spiffyui.client.rest.RESTility;

import com.google.gwt.core.client.GWT;
import com.google.gwt.json.client.JSONValue;

import cz.proks.easyhelpdesk.api.config.Configuration;
import cz.proks.easyhelpdesk.api.model.HelpdeskRole;
import cz.proks.easyhelpdesk.api.model.ThreadMember;
import cz.proks.easyhelpdesk.api.model.ThreadMemberJsonizer;
import cz.proks.easyhelpdesk.gwt.utils.JsonizerUtils;
import cz.proks.easyhelpdesk.gwt.utils.RestUtils;

public class ThreadMemberRestApi extends BaseRestApi<ThreadMember>{
    private final static Jsonizer JSONIZER = GWT.create(ThreadMemberJsonizer.class);

    @Override
    protected String getBaseRestDomainUrlPart() {
        return Configuration.WS.THREAD_MEMBER_BASE_URL;
    }

    @Override
    protected Jsonizer getDomainJsonizer() {
        return JSONIZER;
    }    
    
    public void listForHelpdeskMember(Long helpdeskMemberId, final DomainListCallback<ThreadMember> callback) {
        RestUtils.makeRestRequest(getBaseDomainUrl() + "/listForHelpdeskMember/" + helpdeskMemberId, new RestUtils.Callback(callback) {

            @Override
            public void onSuccess(JSONValue val) {
                Collection<ThreadMember> entities = JsonizerUtils.getListFromJsonArray(val, JSONIZER);
                callback.onDomainListSuccess(entities);                
            }
        });
    }
    
    public void setRead(Long id, boolean isRead, RESTCallback callback) {
        RestUtils.makeRestRequest(getBaseDomainUrl() + "/" + id + "/setRead/" + isRead, null, RESTility.PUT, callback);
    }
    
    public void shareForHelpdeskAndThreadAndRole(final Long helpdeskId, final Long threadId, final HelpdeskRole helpdeskRole, final RESTCallback callback) {
        RestUtils.makeRestRequest(getBaseDomainUrl() + "/shareForHelpdesk/" + helpdeskId + "/ForThread/" + threadId + "/ForRole/" + helpdeskRole.toString(), null, RESTility.PUT, callback);
    }
    
    public void shareForHelpdeskMemberAndRole(Long helpdeskMemberId, HelpdeskRole helpdeskRole, RESTCallback callback) {
        RestUtils.makeRestRequest(getBaseDomainUrl() + "/shareForHelpdeskMember/" + helpdeskMemberId + "/ForRole/" + helpdeskRole.toString(), null, RESTility.PUT, callback);
    }
}
